package com.itheima.tanhua.server.controller;

import com.github.pagehelper.PageHelper;
import com.itheima.commons.utils.JwtUtils;
import com.itheima.tanhua.dubbo.api.MovementsService;
import com.itheima.tanhua.dubbo.api.UserService;
import com.itheima.tanhua.model.dto.MovementsDto;
import com.itheima.tanhua.model.vo.MovementsVo;
import io.jsonwebtoken.Claims;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("movements")
public class MovementsController {
    @DubboReference
    private MovementsService movementsService;

    /**
     * 发布圈子动态
     *
     * @param authorization
     * @param multipartFile
     * @param movementsDto
     */
    @PostMapping
    public void saveMovement(@RequestHeader("Authorization") String authorization, MultipartFile multipartFile, MovementsDto movementsDto) {
        //获取id
        Claims claims = JwtUtils.getClaims(authorization);
        Integer id = (Integer) claims.get("id");
        long l = id.longValue();
        movementsDto.setId(l);
        //存入动态表
        movementsService.saveMovement(movementsDto);
        //获取朋友列表
        List<Integer> friends = movementsService.getFriends(id);
        //获取动态id
        List<Long> mid = movementsService.getMovementId(l);
        //存入朋友动态表
        for (Integer friend : friends) {
            for (Long aLong : mid) {
                movementsService.saveMovementTimeLine(friend, id, aLong);
            }

        }


    }

    /**
     * 我的动态
     * @return
     */
    @GetMapping("/all")
    public List<MovementsVo> getPersonMovement(@RequestHeader("Authorization") String authorization, Integer page, Integer pagesize,@RequestParam(value = "userId",required = false) Long userId) {
        //判断userId是否为空
        if (userId == null){
            Claims claims = JwtUtils.getClaims(authorization);
            Integer id = (Integer) claims.get("id");
            userId = id.longValue();
        }
        //设置分页
        PageHelper.startPage(page,pagesize);
        //查询动态信息
//        List<MovementsVo> movementsService.selectPersonMovement(userId)
        return null;
    }

    /**
     * 查询好友动态
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity getMovements(Integer page, Integer pagesize) {
        //设置分页
        PageHelper.startPage(page, pagesize);
        //查询发布动态的好友
        //查询好友的动态具体信息
        //返回动态
        return null;
    }
}
